from Common.DuplicatedInstanceError import DuplicatedInstanceError
from Common.HITPropertiesTemplate import HITPropertiesTemplate
from Common.Reward import Reward
from Controller.BaseController import BaseController
from Utility import UserRole, FormData
from google.appengine.ext import db
import string

class CreateHitPropertiesTemplateController(BaseController):
    def get(self):
        self.response.out.write("Bad Request")

    def post(self):
        """
        Verfiy user role
            redirect user to login page
        or
            gather user inputs, generate questionform, save HITs to session, redirect USER to preview page
        """
        u = self.GetCurrentUser()
        if u is None:
            self.response.out.write('100');
            return
        if u.Role != UserRole.User:
            self.response.out.write('403');
            return

        templateName = self.request.get(FormData.HIT_TEMPLATE_NAME)
        title = self.request.get(FormData.HIT_TEMPLATE_TITLE)
        description = self.request.get(FormData.HIT_TEMPLATE_DESCRIPTION)
        keywords = self.request.get(FormData.HIT_TEMPLATE_KEYWORDS)

        assignmentDuration = string.atoi(self.request.get(FormData.HIT_TEMPLATE_ASSIGNMENT_DURATION))
        assignmentDurationUnit = self.request.get(FormData.HIT_TEMPLATE_ASSIGNMENT_DURATION_UNIT)

        lifetime = string.atoi(self.request.get(FormData.HIT_TEMPLATE_LIFETIME))
        lifetimeUnit = self.request.get(FormData.HIT_TEMPLATE_LIFETIME_UNIT)

        reward = 0.01 #string.atof(self.request.get(FormData.HIT_TEMPLATE_REWARD))
        maxAssignments = string.atoi(self.request.get(FormData.HIT_TEMPLATE_MXA_ASSIGNMENTS))
#
        autoApprovalDelay = string.atoi(self.request.get(FormData.HIT_TEMPLATE_AUTO_APPROVAL_DELAY))
        autoApprovalDelayUnit = self.request.get(FormData.HIT_TEMPLATE_AUTO_APPROVAL_DELAY_UNIT)

        try:
            hitPropertiesTempate = HITPropertiesTemplate(
                parent=u.Ref_Group,
                key_name=templateName,
                TemplateName=templateName,
                Title=title,
                Description=description,
                Keywords=keywords,
                AssignmentDuration=assignmentDuration,
                AssignmentDurationUnit=assignmentDurationUnit,
                Lifetime=lifetime,
                LifetimeUnit=lifetimeUnit,
                MaxAssignments=maxAssignments,
                AutoApprovalDelay=autoApprovalDelay,
                AutoApprovalDelayUnit=autoApprovalDelayUnit,
                Ref_Company=u.Ref_Company
            )
            rw = Reward(
                parent=hitPropertiesTempate,
                FormattedPrice=reward,
                Amount=long(reward * 100),
                Ref_HIT=hitPropertiesTempate
            )
            db.run_in_transaction(hitPropertiesTempate.put, rw)
            self.response.out.write("Template: " + templateName + " was created successfully!");
        except DuplicatedInstanceError:
            self.response.out.write("Template [" + templateName + "] already exists!")
        except db.TransactionFailedError:
            self.response.out.write("can't not save this template!")
